package edu.bupt.changelog.history.defaultsaver;

import com.google.common.collect.Lists;
import edu.bupt.changelog.history.HistoryRegistion;
import edu.bupt.changelog.history.aop.ChangeSaver;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class DefaultChangeSaver implements ChangeSaver {
    public static List<List<Object>> store = new LinkedList<>();

    @Override
    public void save(Object oldEntity, Object newEntity) {
        if(oldEntity==null && newEntity==null){
            return;
        }

        final Object entity = oldEntity!=null ? oldEntity : newEntity;

        final String entityUniqueKey = HistoryRegistion.tryGetEntityKey(entity)
                    .orElseThrow(() -> new RuntimeException());

        store.add(Lists.newArrayList(entityUniqueKey, Optional.ofNullable(oldEntity),Optional.ofNullable(newEntity)));

    }

}
